*************************************************************
*** RUN JACKKNIFE REGS COMPARING HOSP TO NBRS WITHIN 100M ***
*** Last edited: 4/7/2023         		   	    		  ***
*************************************************************
log using "${CodePath}/log/regressions_100m_$S_DATE.log", text replace

local segmentlengths = "50 100 150"
foreach seglength of local segmentlengths{
	cap mkdir "${OutputPath}/RACRegion/"
	cap mkdir "${OutputPath}/RACRegion/jackknife_audit_in2011/"
	cap mkdir "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/"
	cap mkdir "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Event Studies/"
	cap mkdir "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Event Studies/nearseg`seglength'_state_2__state/"
	cap mkdir "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Event Studies/nearseg`seglength'_state_2__state/CI95/"

	cap mkdir "${OutputPath}/RACRegion/jackknife_denial_in2011/"
	cap mkdir "${OutputPath}/RACRegion/jackknife_denial_in2011/hospcomp_100m/"
	cap mkdir "${OutputPath}/RACRegion/jackknife_denial_in2011/hospcomp_100m/Event Studies/"
	cap mkdir "${OutputPath}/RACRegion/jackknife_denial_in2011/hospcomp_100m/Event Studies/nearseg`seglength'_state_2__state/"
	cap mkdir "${OutputPath}/RACRegion/jackknife_denial_in2011/hospcomp_100m/Event Studies/nearseg`seglength'_state_2__state/CI95/"

	cap mkdir "${OutputPath}/RACRegion/jackknife_audit_in2011/"
	cap mkdir "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/"
	cap mkdir "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Tables/"
	cap mkdir "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Tables/nearseg`seglength'_state_2__state/"

	cap mkdir "${OutputPath}/RACRegion/jackknife_denial_in2011/"
	cap mkdir "${OutputPath}/RACRegion/jackknife_denial_in2011/hospcomp_100m/"
	cap mkdir "${OutputPath}/RACRegion/jackknife_denial_in2011/hospcomp_100m/Tables/"
	cap mkdir "${OutputPath}/RACRegion/jackknife_denial_in2011/hospcomp_100m/Tables/nearseg`seglength'_state_2__state/"

	cap mkdir "${OutputPath}/RACRegion/state_audit_in2011/"
	cap mkdir "${OutputPath}/RACRegion/state_audit_in2011/hospcomp_100m/"
	cap mkdir "${OutputPath}/RACRegion/state_audit_in2011/hospcomp_100m/Event Studies/"
	cap mkdir "${OutputPath}/RACRegion/state_audit_in2011/hospcomp_100m/Event Studies/nearseg`seglength'_state_2__state/"
	cap mkdir "${OutputPath}/RACRegion/state_audit_in2011/hospcomp_100m/Event Studies/nearseg`seglength'_state_2__state/CI95/"



	cap mkdir "${OutputPath}/RACRegion/jackknife_RACregion_audit_in2011/"
	cap mkdir "${OutputPath}/RACRegion/jackknife_RACregion_audit_in2011/hospcomp_100m/"
	cap mkdir "${OutputPath}/RACRegion/jackknife_RACregion_audit_in2011/hospcomp_100m/Event Studies/"
	cap mkdir "${OutputPath}/RACRegion/jackknife_RACregion_audit_in2011/hospcomp_100m/Event Studies/nearseg`seglength'_state_2__state/"
	cap mkdir "${OutputPath}/RACRegion/jackknife_RACregion_audit_in2011/hospcomp_100m/Event Studies/nearseg`seglength'_state_2__state/CI95/"
}

clear

*--------------------------------------------------
* Select and create dataset for regressions
*--------------------------------------------------
local hospvars    "log_n_claims  log_tot_pmt_amt  log_real_gcost_admin_net  app_contracting_136 log_n_claims_02 log_tot_pmt_amt_02 log_n_claims_over02 log_tot_pmt_amt_over02 l_drg_anytop20 l_n_claims_notop20 l_drg_top20_emergent l_drg_top20_nonemergent l_drg_anytop20_pmt obs_oplongED_ratio die_30d revisit_30d DemandAmt_3y_1000s log_n_dx" 

local keepvars "hospprofittype sh_claims_02_2010 teaching_2010 independent_2010 app_contracted_136_2010 urban beds_2010"



use "${DataPath}/within100m/pnlist_groups_100m.dta", clear
duplicates drop hospcomp_group_100 pn, force
expand 9
sort pn
bys pn hospcomp_group_100 : gen fyear = _n + 2006
// create a small dataset with relevant vars
preserve
	use "${DataPath}/derived/hospyear_0716_jk.dta", clear
	keep if !missing(RACregion_audit_in2011) //restrict to acute hospitals
	// define above avg beds
	sum beds_2010 if fyear == 2010, detail
	local med_beds = r(p50)
	gen above_avg_beds_2010 = beds_2010 >= `med_beds'
	label define aboveavgbeds_l 0 "below avg" 1 "above avg beds"
	label values above_avg_beds_2010 aboveavgbeds_l

	// label chain vs. non-chain
	label define independent_l 0 "chain" 1 "non-chain"
	label values independent_2010 independent_l

	// high vs. low 2010 audit
	sum audit_in2010 if fyear == 2010, detail
	local med_audit_2010 = r(p50)
	gen above_avg_audit_in2010 = audit_in2010 >= `med_audit_2010'
	local med_audit_2010 = round(`med_audit_2010', 0.1)
	label define aboveavgaudit2010_l 0 "below avg" 1 "above avg 2010 audit (`med_audit_2010')"
	label values above_avg_audit_in2010 aboveavgaudit2010_l

	// above avg admin share
	gen temp = share_gcost_admin_net if fyear == 2010
	bys pn: egen share_gcost_admin_2010 = mean(temp)
	drop temp
	sum share_gcost_admin_net if fyear == 2010, detail
	local med_admin_share_2010 = r(p50)
	gen above_avg_admin_share_2010 = share_gcost_admin_2010 >= `med_admin_share_2010'

	// above avg short stay share
	sum sh_claims_02_2010, detail
	local med_sh_claims_2010 = r(p50)
	gen above_avg_sh_claims_02_2010 = sh_claims_02_2010 >= `med_sh_claims_2010'

	// above avg top 20 error drg
	gen temp = sh_n_claims_top20error if fyear == 2010
	bys pn: egen sh_n_claims_top20error_2010 = mean(temp)
	drop temp
	sum sh_n_claims_top20error if fyear == 2010, detail
	local med_sh_top20error_2010 = r(p50)
	gen above_avg_sh_top20error_2010 = sh_n_claims_top20error_2010 > `med_sh_top20error_2010'

	bys RACregion: egen temp = mean(demandafterauditrate_in2011) if fyear == 2011
	bys pn (fyear): egen RACavg_demandafteraudit_in2011 = mean(temp)
	drop temp
	bys state: egen temp = mean(demandafterauditrate_in2011) if fyear == 2011
	bys pn (fyear): egen stateavg_demandafteraudit_in2011 = mean(temp)
	drop temp
	
	keep pn fyear dist_to_border RACregion_audit* jk_RACregion_audit_in2011* jk_state_audit_in2011* state_audit_in2011* audit_in2011* jk_RACregion_audit_in2011* demandrate_in2011* jk_RACregion_demand_in2011*  jk_state_demand_in2011* `hospvars' `keepvars' nearseg100_state_2 nearseg100_state RACregion state demandafterauditrate_in2011  above_avg_beds_2010 

	keep if fyear >= 2007 & fyear <= 2015
	tempfile temp
	save `temp'
restore

merge m:1 pn fyear using `temp', keep(1 3) gen(_jkmerge)

egen hospcomp_group_fyear = group(hospcomp_group_100 fyear)
egen state_splitid = group(state split_id)
egen state_nearseg100 = group(state nearseg100_state)

// convert demanded payment into 1000s
cap gen DemandAmt_3y_1000 = DemandAmt_3y/1000
cap label var DemandAmt_3y_1000 "net amount refunded (thousand $)" 

*--------------------------------------------------
* Make event studies
*--------------------------------------------------
foreach hospvar of varlist `hospvars' {

			// jackknife state audit rate as instrument
				reghdfe  `hospvar' jk_state_audit_in2011_X2*, absorb(pn hospcomp_group_fyear) cluster(nearseg100_state_2 state)
				event_study95 jk_state_audit_in2011
				graph export "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/`hospvar'.png", replace
				ivreghdfe  `hospvar' (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
			 			= jk_state_audit_in2011_X2007 jk_state_audit_in2011_X2008 jk_state_audit_in2011_X2009 jk_state_audit_in2011_X2011 jk_state_audit_in2011_X2012 jk_state_audit_in2011_X2013 jk_state_audit_in2011_X2014 jk_state_audit_in2011_X2015) ///
						,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)
				iv_event_study95 audit_in2011
				graph export "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/`hospvar'_IV.png", replace 

			// jackknife state denial rate as instrument
				reghdfe  `hospvar' jk_state_demand_in2011_X2*, absorb(pn hospcomp_group_fyear) cluster(nearseg100_state_2 state)
				event_study95 jk_state_demand_in2011
				graph export "${OutputPath}/RACRegion/jackknife_denial_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/`hospvar'.png", replace
			 
				ivreghdfe  `hospvar' (demandrate_in2011_X2007 demandrate_in2011_X2008 demandrate_in2011_X2009 demandrate_in2011_X2011 demandrate_in2011_X2012 demandrate_in2011_X2013 demandrate_in2011_X2014 demandrate_in2011_X2015 ///
			 			= jk_state_demand_in2011_X2007 jk_state_demand_in2011_X2008 jk_state_demand_in2011_X2009 jk_state_demand_in2011_X2011 jk_state_demand_in2011_X2012 jk_state_demand_in2011_X2013 jk_state_demand_in2011_X2014 jk_state_demand_in2011_X2015) ///
						,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)
				iv_event_study95 demandrate_in2011
				graph export "${OutputPath}/RACRegion/jackknife_denial_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/`hospvar'_IV.png", replace   


	// log_n_claims robustness checks and yscaled
	if ("`hospvar'"== "log_n_claims"){

		// state audit rate as instrument
				reghdfe  `hospvar' state_audit_in2011_X2*, absorb(pn hospcomp_group_fyear) cluster(nearseg100_state_2 state)
				event_study95 state_audit_in2011
				graph export "${OutputPath}/RACRegion/state_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/`hospvar'.png", replace

				ivreghdfe  `hospvar' (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
			 			= state_audit_in2011_X2007 state_audit_in2011_X2008 state_audit_in2011_X2009 state_audit_in2011_X2011 state_audit_in2011_X2012 state_audit_in2011_X2013 state_audit_in2011_X2014 state_audit_in2011_X2015) ///
						,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)
				iv_event_study95 audit_in2011
				graph export "${OutputPath}/RACRegion/state_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/`hospvar'_IV.png", replace 

			// jackknife RAC region audit rate as instrument
				reghdfe  `hospvar' jk_RACregion_audit_in2011_X2*, absorb(pn hospcomp_group_fyear) cluster(nearseg100_state_2 state)
				event_study95 jk_RACregion_audit_in2011
				graph export "${OutputPath}/RACRegion/jackknife_RACregion_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/`hospvar'.png", replace

				ivreghdfe  `hospvar' (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
			 			= jk_RACregion_audit_in2011_X2007 jk_RACregion_audit_in2011_X2008 jk_RACregion_audit_in2011_X2009 jk_RACregion_audit_in2011_X2011 jk_RACregion_audit_in2011_X2012 jk_RACregion_audit_in2011_X2013 jk_RACregion_audit_in2011_X2014 jk_RACregion_audit_in2011_X2015) ///
						,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)
				iv_event_study95 audit_in2011
				graph export "${OutputPath}/RACRegion/jackknife_RACregion_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/`hospvar'_IV.png", replace 
				
			

		// intensive margin (remove if audit rate = 0)
		ivreghdfe  `hospvar' (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
				 			= jk_state_audit_in2011_X2007 jk_state_audit_in2011_X2008 jk_state_audit_in2011_X2009 jk_state_audit_in2011_X2011 jk_state_audit_in2011_X2012 jk_state_audit_in2011_X2013 jk_state_audit_in2011_X2014 jk_state_audit_in2011_X2015) ///
							if audit_in2011 != 0 ///
							,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)
					iv_event_study95_yscale audit_in2011 -0.06 0.03 0.06
					graph export "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/`hospvar'_IV_int.png", replace 

		// baseline specs, yscaled
		ivreghdfe  `hospvar' (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
				 			= jk_state_audit_in2011_X2007 jk_state_audit_in2011_X2008 jk_state_audit_in2011_X2009 jk_state_audit_in2011_X2011 jk_state_audit_in2011_X2012 jk_state_audit_in2011_X2013 jk_state_audit_in2011_X2014 jk_state_audit_in2011_X2015) ///
							,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2) 
		iv_event_study95_yscale audit_in2011 -0.06 0.03 0.06
		graph export "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/`hospvar'_IV_yscale.png", replace 

		// jacknife RAC region 
		ivreghdfe  `hospvar' (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
	 			= jk_RACregion_audit_in2011_X2007 jk_RACregion_audit_in2011_X2008 jk_RACregion_audit_in2011_X2009 jk_RACregion_audit_in2011_X2011 jk_RACregion_audit_in2011_X2012 jk_RACregion_audit_in2011_X2013 jk_RACregion_audit_in2011_X2014 jk_RACregion_audit_in2011_X2015) ///
				,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)
		iv_event_study95_yscale audit_in2011 -0.06 0.03 0.06
		graph export "${OutputPath}/RACRegion/jackknife_RACregion_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/`hospvar'_IV_yscale.png", replace 

		// state audit rate instrument
		ivreghdfe  `hospvar' (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
	 			= state_audit_in2011_X2007 state_audit_in2011_X2008 state_audit_in2011_X2009 state_audit_in2011_X2011 state_audit_in2011_X2012 state_audit_in2011_X2013 state_audit_in2011_X2014 state_audit_in2011_X2015) ///
				,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)
		iv_event_study95_yscale audit_in2011 -0.06 0.03 0.06
		graph export "${OutputPath}/RACRegion/state_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/`hospvar'_IV_yscale.png", replace  

	} // yscaled robustness checks


	//yscaled for top vs non-top 20
	if "`hospvar'" == "l_n_claims_notop20" | "`hospvar'" == "l_drg_anytop20" | "`hospvar'" == "l_drg_top20_nonemergent" | "`hospvar'" == "l_drg_top20_emergent" {
		local label : variable label `hospvar'
		ivreghdfe  `hospvar' (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
				= jk_state_audit_in2011_X2007 jk_state_audit_in2011_X2008 jk_state_audit_in2011_X2009 jk_state_audit_in2011_X2011 jk_state_audit_in2011_X2012 jk_state_audit_in2011_X2013 jk_state_audit_in2011_X2014 jk_state_audit_in2011_X2015) ///
			if fyear >= 2008,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2) 
			
		iv_event_study95_yscale audit_in2011 -0.15 0.05 0.05
		graph export "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/`hospvar'_IV_yscale.png", replace 
	}

	//yscaled for top vs non-top 20
	if "`hospvar'" == "l_n_claims_notop20" | "`hospvar'" == "l_drg_anytop20"  {
		local label : variable label `hospvar'
		ivreghdfe  `hospvar' (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
				= jk_state_audit_in2011_X2007 jk_state_audit_in2011_X2008 jk_state_audit_in2011_X2009 jk_state_audit_in2011_X2011 jk_state_audit_in2011_X2012 jk_state_audit_in2011_X2013 jk_state_audit_in2011_X2014 jk_state_audit_in2011_X2015) ///
			if fyear >= 2008,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2) 
			
		iv_event_study95_yscale audit_in2011 -0.06 0.04 0.02
		graph export "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/`hospvar'_IV_yscale2.png", replace 
	}

	// yscaled for share from ED
	if strpos("`hospvar'", "obs_oplongED_ratio") > 0 {
		ivreghdfe  `hospvar' (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
				= jk_state_audit_in2011_X2007 jk_state_audit_in2011_X2008 jk_state_audit_in2011_X2009 jk_state_audit_in2011_X2011 jk_state_audit_in2011_X2012 jk_state_audit_in2011_X2013 jk_state_audit_in2011_X2014 jk_state_audit_in2011_X2015) ///
			if fyear >= 2008,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2) 
			
		iv_event_study95_yscale audit_in2011 -0.01 0.01 0.005
	}
} // hospvar




*--------------------------------------------------
* Event study tables
*--------------------------------------------------

bys pn fyear: gen first = _n == 1

label var log_n_claims "log n Medicare inpatient claims"
label var log_tot_pmt_amt "log Medicare inpatient revenue"

label var log_n_claims_02 "log n Medicare inpatient claims, LOS 0-2"
label var log_tot_pmt_amt_02 "log Medicare inpatient revenue, LOS 0-2"

label var log_n_claims_over02 "log n Medicare inpatient claims, LOS \> 2"
label var log_tot_pmt_amt_over02 "log Medicare inpatient revenue, LOS \> 2"


label var l_drg_anytop20 "log n Medicare inpatient claims, top 20 error DRGs"
label var l_drg_anytop20_pmt "log n Medicare inpatient payment, top 20 error DRGs"
label var l_n_claims_notop20 "log n Medicare inpatient claims, non-top 20 error DRGs"

label var audit_in2011_X2011 "2011 audit rate $\times$ 2011"
label var audit_in2011_X2012 "2011 audit rate $\times$ 2012"
label var audit_in2011_X2013 "2011 audit rate $\times$ 2013"
label var audit_in2011_X2014 "2011 audit rate $\times$ 2014"
label var audit_in2011_X2015 "2011 audit rate $\times$ 2015"

label var audit_in2011_Xpost "2011 audit rate $\times$ post"

label var jk_state_audit_in2011_X2011 "2011 jackknife state audit rate $\times$ 2011"
label var jk_state_audit_in2011_X2012 "2011 jackknife state audit rate $\times$ 2012"
label var jk_state_audit_in2011_X2013 "2011 jackknife state audit rate $\times$ 2013"
label var jk_state_audit_in2011_X2014 "2011 jackknife state audit rate $\times$ 2014"
label var jk_state_audit_in2011_X2015 "2011 jackknife state audit rate $\times$ 2015"

label var audit_in2011 "2011 audit rate"
label var jk_state_audit_in2011 "2011 jackknife state audit rate"
label var app_contracting_136 "install med. necc. app"


// main table: 2sls n claims, n revenue, n claims <= 2, inpatient rev <= 2, top 20 DRGs
	eststo clear
	ivreghdfe  log_n_claims (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
				 			= jk_state_audit_in2011_X2007 jk_state_audit_in2011_X2008 jk_state_audit_in2011_X2009 jk_state_audit_in2011_X2011 jk_state_audit_in2011_X2012 jk_state_audit_in2011_X2013 jk_state_audit_in2011_X2014 jk_state_audit_in2011_X2015) ///
							,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)


	local outvars "log_n_claims log_tot_pmt_amt log_n_claims_02 log_tot_pmt_amt_02 l_drg_anytop20 l_drg_anytop20_pmt"
	foreach outvar of varlist `outvars'{
		ivreghdfe  `outvar' (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
				 			= jk_state_audit_in2011_X2007 jk_state_audit_in2011_X2008 jk_state_audit_in2011_X2009 jk_state_audit_in2011_X2011 jk_state_audit_in2011_X2012 jk_state_audit_in2011_X2013 jk_state_audit_in2011_X2014 jk_state_audit_in2011_X2015) ///
							,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)
		eststo m_`outvar'
		distinct pn if e(sample)
		estadd local hosp = r(ndistinct) 
		estadd local fstat =round(`e(rkf)', 0.01)
		estadd local pnFE          "X"
		estadd local nbrFE     "X"
	}


	esttab using "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Tables/nearseg100_state_2__state/tab_IV_claims1_v2.tex", ///
		 b(%9.4f) keep(*X2011 *X2012 *X2013 *X2014 *X2015) ///
		 stats(pnFE nbrFE hosp N fstat, labels("Hosp FE" "Nbr group FE" "Hosp" "N" "F") fmt(%9.0g %9.0g %9.1g)) ///
		 starlevels(* 0.10 ** 0.05 *** 0.01) se replace label

// first stage and main results
	// IV
	eststo clear

	local outvars "log_n_claims log_tot_pmt_amt log_n_claims_02 log_tot_pmt_amt_02 log_real_gcost_admin_net app_contracting_136"
	foreach outvar of varlist `outvars'{
		ivreghdfe  `outvar' (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
				 			= jk_state_audit_in2011_X2007 jk_state_audit_in2011_X2008 jk_state_audit_in2011_X2009 jk_state_audit_in2011_X2011 jk_state_audit_in2011_X2012 jk_state_audit_in2011_X2013 jk_state_audit_in2011_X2014 jk_state_audit_in2011_X2015) ///
							,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)
		eststo m_`outvar'
		distinct pn if e(sample)
		estadd local hosp = r(ndistinct) 
		estadd local fstat =round(`e(rkf)', 0.01)
		estadd local pnFE          "X"
		estadd local nbrFE     "X"
	}


	esttab using "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Tables/nearseg100_state_2__state/tab_IV_maintable_v2.tex", ///
		 b(%9.4f) keep(*X2011 *X2012 *X2013 *X2014 *X2015) ///
		 stats(pnFE nbrFE hosp N fstat, labels("Hosp FE" "Nbr group FE" "Hosp" "N" "F") fmt(%9.0g %9.0g %9.1g)) ///
		 starlevels(* 0.10 ** 0.05 *** 0.01) se replace label

*--------------------------------------------------
* XPost Tables
*--------------------------------------------------
// Overall - IV
eststo clear

local outvars "log_n_claims log_tot_pmt_amt log_n_claims_02 log_tot_pmt_amt_02 log_real_gcost_admin_net app_contracting_136"
foreach outvar of varlist `outvars'{
	ivreghdfe  `outvar' (audit_in2011_Xpost ///
				 			= jk_state_audit_in2011_Xpost) ///
						,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)
	eststo m_`outvar'
	distinct pn if e(sample)
	estadd local hosp = r(ndistinct) 
	estadd local fstat =round(`e(rkf)', 0.01)
	estadd local pnFE          "X"
	estadd local nbrFE     "X"
}

esttab using "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Tables/nearseg100_state_2__state/tab_postcoeff.tex", ///
	 b(%9.4f) keep(audit_in2011_Xpost) ///
	 stats(pnFE nbrFE hosp N fstat, labels("Hosp FE" "Nbr group FE" "Hosp" "N" "F") fmt(%9.0g %9.0g %9.1g)) ///
	 starlevels(* 0.10 ** 0.05 *** 0.01) se replace label

// Overall - reduced form
eststo clear


// Heterogeneity
eststo clear
local heterovars "`heterovars' urban teaching hospprofittype independent_2010 above_avg_beds_2010 app_contracted_136_2010 "

// IV
	foreach heterovar of local heterovars{
		foreach hospvar of varlist log_n_claims log_tot_pmt_amt log_n_claims_02 log_tot_pmt_amt_02 log_real_gcost_admin_net app_contracting_136{
		table_hetero audit_in2011 jk_state_audit_in2011 `heterovar' `hospvar'
		distinct pn if e(sample)
		estadd local hosp = r(ndistinct) 
		estadd local pnFE          "X"
		estadd local nbrFE     "X"
		estadd local fstat =round(`e(rkf)', 0.01)
		eststo m_`hospvar'
		} //hospvar
		esttab using "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Tables/nearseg100_state_2__state/tab_postcoeff_hetero_`heterovar'.tex", ///
	 		b(%9.4f) keep(audit_in2011_Xpost* t_*) label replace  ///
			 stats(pnFE nbrFE hosp N fstat, labels("Hosp FE" "Nbr group FE" "Hosp" "N" "F") fmt(%9.0g %9.0g %9.1g)) ///
			 starlevels(* 0.10 ** 0.05 *** 0.01) se
	} //heterovar

*--------------------------------------------------
* Event studies with controls
*--------------------------------------------------
// make control variables
egen above_avg_bed_fyear = group(above_avg_beds_2010 fyear)
egen urban_fyear = group(urban fyear)
egen profittype_fyear = group(hospprofittype fyear)
egen teaching_fyear = group(teaching_2010 fyear)
egen independent_fyear = group(independent_2010 fyear)
egen above_avg_admin_share_fyear = group(above_avg_admin_share_2010 fyear)
egen above_avg_sh_claims_02_fyear = group(above_avg_sh_claims_02_2010 fyear)
egen above_avg_sh_top20error_fyear = group(above_avg_sh_top20error_2010 fyear)


foreach hospvar of varlist log_n_claims log_tot_pmt_amt log_real_gcost_admin_net app_contracting_136 log_n_claims_02 log_tot_pmt_amt_02 l_n_pred_shortstay log_tot_pmt_amt_over02 log_n_claims_over02 l_drg_anytop20 l_n_claims_notop20{
	ivreghdfe  `hospvar' (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
 			= jk_state_audit_in2011_X2007 jk_state_audit_in2011_X2008 jk_state_audit_in2011_X2009 jk_state_audit_in2011_X2011 jk_state_audit_in2011_X2012 jk_state_audit_in2011_X2013 jk_state_audit_in2011_X2014 jk_state_audit_in2011_X2015) ///
			,absorb(hospcomp_group_fyear pn above_avg_bed_fyear urban_fyear profittype_fyear teaching_fyear independent_fyear above_avg_admin_share_fyear above_avg_sh_claims_02_fyear above_avg_sh_top20error_fyear) cluster(state nearseg100_state_2)

	iv_event_study95 audit_in2011	
	graph export "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Event Studies/nearseg100_state_2__state/CI95/`hospvar'_IV_controls.png", replace 
} //hospvar

log close
